﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//移动的坦克
public class MoveTank : MonsterTank
{
    //1.坦克在2个点之间 来回的移动
    private Transform targetPos;
    //随机用的点 外面去关联
    public Transform[] randomPos;

    //2.坦克要一直盯着自己的目标
    public Transform lookAtTarget;

    public Texture maxHpBk;
    public Texture hpBk;

    private Rect maxHpRect = new Rect(0,0,50,10);
    private Rect hpRect = new Rect(0, 0, 50, 10);
    public Rect hpOffsetRect;

    //显示血条计时时间
    private float showHpTime;

    private void Start()
    {
        RandomPos();
    }

    private void RandomPos()
    {
        if (randomPos.Length == 0)
            return;
        int index = Random.Range(0, randomPos.Length);
        targetPos = randomPos[index];
    }

    public bool CheckBattle()
    {
        if (Vector3.Distance(this.transform.position, lookAtTarget.position) < 10f)
        {
            return true;
        }
        return false;
    }

    public override void GameLoop()
    {
        #region 多个点之间的随机移动
        //看向自己的目标点
        this.transform.LookAt(targetPos);
        //不停的向自己的面朝向位移
        this.transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

        //知识点 Vector3里面有一个得到两个之间距离的方法
        //当距离过小时 认为到达了目的地 重新随机一个点
        if (Vector3.Distance(this.transform.position, targetPos.position) < 0.05f)
        {
            RandomPos();
        }
        #endregion

        #region 看向自己的目标
        if (lookAtTarget)
        {
            Head.LookAt(lookAtTarget);
            if(CheckBattle())
                NowTime += Time.deltaTime;
            if (CheckFire())
            {
                Fire();
            }
        }
        #endregion
    }

    public override void Dead()
    {
        base.Dead();
        GamePanel.Instance.AddScore(10);
    }

    public override void Wound(TankBase other)
    {
        base.Wound(other);
        showHpTime = 2;
    }

    private void OnGUI()
    {
        if (showHpTime > 0)
        {
            //减少显示时间
            showHpTime -= Time.deltaTime;
            //画图,画血条
            //1.把怪物的当前位置 转换成屏幕位置 
            //摄像机里提供了API将世界坐标转为屏幕坐标
            Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position);
            //2.屏幕位置转换成 GUI位置
            //知识点：得到当前屏幕的分辨率高
            screenPos.y = Screen.height - screenPos.y;
            //然后再绘制
            //知识点：GUI中的 图片绘制 
            maxHpRect.x = screenPos.x + hpOffsetRect.x;
            maxHpRect.y = screenPos.y + hpOffsetRect.y;
            GUI.DrawTexture(maxHpRect, maxHpBk);

            hpRect.x = screenPos.x + hpOffsetRect.x;
            hpRect.y = screenPos.y + hpOffsetRect.y;
            hpRect.width = maxHpRect.width * ((float)hp / (float)maxHp);
            GUI.DrawTexture(hpRect, hpBk);
        }
    }
}


